<!--
User: ${author}
Date: ${date}
view: ${table.comment!} 添加
-->
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
         contentType="text/html;charset=utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
<head>
    <title>${table.comment!}添加</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.5.4/css/layui.css"/>
</head>
<body>
<form style="margin-top: 20px;margin-left: 20px" class="layui-form layui-form-pane" action="">

<#if fieldList?exists>
    <#list fieldList?keys as key>
        <div class="layui-form-item">
            <#assign fieldLists = fieldList[key]>
            <#if fieldLists??>
                <#list fieldLists! as field>
                <label class="layui-form-label"
                       style="width: 150px"><#if field.comment!?length gt 0>${field.comment}<#else >${field.propertyName}</#if></label>
                <div class="layui-input-inline">
                    <input class="layui-input" <#if field.notNull>lay-verify="required"</#if> name="${field.propertyName}"
                           id="${field.propertyName}">
                </div>
                </#list>
            </#if>
        </div>
    </#list>
</#if>
    <div class="layui-form-item">
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" style="margin-top: 20px;" lay-submit="" type="button" lay-filter="submitForm">
                    立即保存
                </button>
            </div>
        </div>
    </div>
</form>
</body>
</html>
<script src="https://www.layuicdn.com/layui-v2.5.4/layui.js"></script>
<script>
    layui.use(['form', 'laydate', "layer"], function () {
        var $ = layui.jquery, layer = layui.layer, form = layui.form , path = "<%=basePath%>";

        form.on('submit(submitForm)', function (data) {
            let field = data.field;
            let indexLoad;
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: path + "/${table.capitalEntityName}/add${table.entityName}",
                data: field,
                traditional: true,
                beforeSend: function () {
                    indexLoad = layer.msg('请稍候……', {
                        icon: 16,
                        shade: 0.01,
                        shadeClose: false,
                        time: 120000
                    });
                },
                success: function (res) {
                    if (res.status === 200) {
                        layer.msg("添加成功..", {icon: 6});
                        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layui.layer.close(index); //再执行关闭
                        parent.layui.table.reload('listReload');
                    } else {
                        layer.msg(res.msg, {icon: 5, shift: 6});
                    }
                }, error: function (e) {
                    layer.msg('请求异常，请重试', {icon: 5, shift: 6});
                },
                complete: function () {
                    layer.close(indexLoad);
                }
            });
        });

    });
</script>
